package com.itheima.web.usermanager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManeger {
    @DubboReference
    private UserInfoService userInfoService;
    //用户信息查询
    public ResponseEntity findUserInfoByPage(Integer pageNum, Integer pageSize) {
       PageBeanVo pageBeanVo = userInfoService.findAll(pageNum,pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }
    @DubboReference
    private MovementService movementService;
 //用户动态查询
    public ResponseEntity findMovementVoByPage(Integer state, Long userId, Integer pageNum, Integer pageSize) {
       PageBeanVo pageBeanVo= movementService.findMovementVoByPage(state,userId,pageNum,pageSize);
        // 2.封装vo
        // 2.1 声明voList
        List<MovementVo> voList = new ArrayList<>();
        // 2.2 获取movementList
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        // 2.3 遍历movementList
        if (CollectionUtil.isNotEmpty(movementList)) {
            for (Movement movement : movementList) {
                // 创建vo
                MovementVo vo = new MovementVo();
                vo.setUserInfo(userInfoService.findUserInfoById(movement.getUserId()));
                vo.setMovement(movement);
                // 指定动态时间
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

                // 添加到集合
                voList.add(vo);
            }
        }
        // 3.将vo设置到分页对象
        pageBeanVo.setItems(voList);

        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);


    }
//用户资料查询
    public ResponseEntity findUserInfoById(Long userId) {
        // 1.调用rpc查询
        UserInfo userInfo = userInfoService.findUserInfoById(userId);
        // 2.返回
        return ResponseEntity.ok(userInfo);
    }

//动态详情
    public ResponseEntity findMovementVoById(String publishId) {
        Movement movement = movementService.findByid(new ObjectId(publishId));
        MovementVo movementVo = new MovementVo();
        if (movement!=null) {
            movementVo.setUserInfo(userInfoService.findUserInfoById(movement.getUserId()));
            movementVo.setMovement(movement);
            movementVo.setCreateDate(DateUtil.formatDateTime(new Date(System.currentTimeMillis())));
        }
        return ResponseEntity.ok(movementVo);
    }
    @DubboReference
    private CommentService commentService;
    //消息列表
    public ResponseEntity findCommentVoByPage(String publishId, Integer pageNum, Integer pageSize) {
        //根据id动态
        PageBeanVo pageBeanVo = commentService.findByPage(publishId, pageNum, pageSize, 2);
        ArrayList<CommentVo> voList = new ArrayList<>();
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 创建vo
                CommentVo vo = new CommentVo();
                vo.setId(comment.getId().toHexString());// 评论id
                UserInfo userInfo = userInfoService.findUserInfoById(comment.getUserId());
                vo.setAvatar(userInfo.getAvatar()); // 评论人头像
                vo.setNickname(userInfo.getNickname()); // 评论人
                vo.setContent(comment.getContent()); // 评论内容
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 评论时间

                // 添加到集合
                voList.add(vo);
            }
        }
        // 3.将vo设置到分页对象
        pageBeanVo.setItems(voList);
        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }
    @DubboReference
    private VideoService videoService;
//视屏列表
    public ResponseEntity findVideos(Long userId, Integer pageNum, Integer pageSize) {
        //根据id 查询视屏动态
        PageBeanVo pageBeanVo = videoService.findVideoByUserId(userId, pageNum, pageSize);
        ArrayList<VideoVo> Volist = new ArrayList<>();
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                VideoVo videoVo = new VideoVo();
                videoVo.setUserInfo(userInfoService.findUserInfoById(video.getUserId()));
                videoVo.setVideo(video);
                Volist.add(videoVo);

            }
        }
        pageBeanVo.setItems(Volist);

return ResponseEntity.ok(pageBeanVo);
    }
}
